<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>resource-view-config-common</title>
    <%@ include file="../include/common.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/table.jsp"%>
	<script type="text/javascript">
	
	function datagrid(){
		var oTable=	$('#viewList').dataTable();
		oTable.fnPageChange("first",true);
	}
	function cancelAllCheck(){
		$("#dt_check_all").removeAttr("checked");
	}
	 //全选和取消全选
	    function allClick(isSigle)
	    {
	    	if(isSigle!=1) //如果是单选就不支持全选
  	    	{
  	    		var dt_check_all = $('#dt_check_all').attr("checked");
	  	    	var allCheckbox = $("input[name='dt_check']");
	  	    	if(dt_check_all)
	  	    	{
	  	    		
	  	    		allCheckbox.attr("checked",allCheckbox);  //全选
	  	    		$('#viewList tbody tr').addClass('row_selected');  //背景色全选
	  	    	}else
	  	    	{
	  	    		allCheckbox.attr("checked",false);   //取消全选
	  	    		$('#viewList tbody tr').removeClass('row_selected');  //背景色取消
	  	    	}
  	    	}
	    }
	$(function(){
		//加载grid
		var oTable=	$('#viewList').dataTable({
			"bProcessing": true, 
			"bServerSide": true,
			"sServerMethod": 'POST',
			"sAjaxSource":'${pageContext.request.contextPath}/config/resource-view-config!list.action',
			"bPaginate": true,
			"bSort": true,
			"bFilter":false,
			"bJQueryUI":false,
			"sPaginationType": "full_numbers",
			//"sScrollX":'100%',
			"sDom":'rt <"bottom"flpi>',
			"iDisplayLength":20,
			"aLengthMenu": [[20, 50, 100], [20, 50,100]],
			"aoColumns":[
				{"mDataProp":"checkbox","bSortable": false},
				{"mDataProp":"op","":"center","bSortable": false},
				{"mDataProp":"name","bSortable": false},
				{"mDataProp":"expandLevel","bSortable": false},
				{"mDataProp":"id","bVisible":false}
			], 
			 "oLanguage": { 
			 				"sLengthMenu": "每页 _MENU_ 条",
			 		 		"sZeroRecords": "  ",
							"sInfo": "当前从 _START_ 到 _END_ 条,共 _TOTAL_ 条记录", 
							"sInfoEmpty": "没有找到记录",
							"oPaginate":{"sFirst":"首页","sPrevious":"上一页","sNext":"下一页","sLast":"尾页"}	
			},
		    "fnServerParams":function(aoData){
		    	var ip = $('#ip').val();
		    	if(ip!='')
		    	{
		    		aoData.push({'name':'ip','value':ip});
		    	}
		    }
	});
	
	
		$('#viewList tbody td :checkbox').live('click', function (e) {
			var nTr = $(this).parents('tr')[0]; 
		    	showDetailFlag = true;
			   if ($(nTr).hasClass('row_selected')) {
				  	$(nTr).removeClass('row_selected'); 
				  	cancelAllCheck(); //取消全选按钮
			  }else
			  {
			  		$(nTr).addClass('row_selected'); 
			 }
			 if(!$(this).attr("checked")){
			 	$('#allCheck').attr("checked",false);
			 }
		});
		showDetailFlag = false;
	
	});
	function edit(id){
		$.ajax({
	  		type : "POST",
	  		url : "${pageContext.request.contextPath}/config/resource-view-config!checkDeleted.action", //请求的action
	  		data : {id:id}, //传的参数
	  		dataType : 'json',
	  		success : function(data) { //结果
	  			if(data.flag == true){
	  				var url ="iframe:config/resource-view-config!input.action?id="+id;
	  				var opt = { title: "修改资源视图",width:530,height:310,buttons: {}};
	  				getTopWin(window).openjBox(url,opt,'update-resource-view-config',window);
	  			}else{
	  				getTopWin(window).getMessageBox().alert("此条视图已经删除","提示");
	  				datagrid();
	  			}
	  		}
	  	});
	}
	function deleteOne(id){
		if(id==1){
			getTopWin(window).getMessageBox().tip("基础架构视图不能被删除。不能删除","提示");
		}
		var submit = function (v, h, f) {
		    if (v == 'ok') {
		    	$.ajax({
		    		type : "POST",
		    		url : "${pageContext.request.contextPath}/config/resource-view-config!delete.action", //请求的action
		    		data : {ids:id}, //传的参数
		    		dataType : 'json',
		    		success : function(data) { //结果
		    			if(data == true){
		    				window.setTimeout(function () { $.jBox.tip('删除成功。', 'success'); }, 100);
							datagrid();
		    			}
		    			else if(data == false){
		    				getTopWin(window).getMessageBox().alert("视图下已经有资源了，不能删除","提示");
		    			}
		    			else{
							getTopWin(window).getMessageBox().alert("此条视图已经删除","提示");
							datagrid();
						}
		    		}
		    	});
		    }
		    else if (v == 'cancel') {
		    }
		    return true; //close
		};
		getTopWin(window).jBox.confirm("确定要删除数据吗？", "提示", submit);
	}
	//得到当前选中的
		function fnGetSelected() {   
			var selections = $('#viewList tr.row_selected');
		    return selections;
	    } 
		function add() {
			
			var url ="iframe:config/resource-view-config!add.action";
			var opt = { title: "新增资源视图",width:500,height:150,buttons: {}};
			
			getTopWin(window).openjBox(url,opt,'add-resource-view-config',window);
			
		}
		function changeDefault(id){
			$.ajax({
	    		type : "POST",
	    		url : "${pageContext.request.contextPath}/config/resource-view-config!changeDefault.action", //请求的action
	    		data : "id="+id, //传的参数
	    		dataType : 'json',
	    		success : function(data) { //结果
					datagrid();
	    		}
	    	});
		}
		function batchDelete(){
			var rows = fnGetSelected();
			if(rows.length>0){
				var submit = function (v, h, f) {
				    if (v == 'ok') {
				        var mydata = {};
						mydata.Ids = new Array(rows.length);
						var oTable =$('#viewList').dataTable();
						for(var i = 0 ; i<rows.length;i++){
							var aData = oTable.fnGetData(rows[i]);
							if(aData.id==1){
								getTopWin(window).getMessageBox().tip("基础架构视图不能被删除。不能删除","提示");
							}
							mydata.Ids[i] = aData.id;
							
						}
						$.ajax({
							type : "POST",
							url : "${pageContext.request.contextPath}/config/resource-view-config!delete.action", //请求的action
							data : jQuery.param(mydata,true), //传的参数
							//data:{"ids":mydata.Ids},
							dataType : 'json',
							success : function(data) { //结果
								if(data == true){
									window.setTimeout(function () { $.jBox.tip('删除成功。', 'success'); }, 100);
									datagrid();
								}
								else if(data == false){
				    				getTopWin(window).getMessageBox().tip("批量删除的视图下已经有资源了，不能删除","提示");
				    			}
								else{
									getTopWin(window).getMessageBox().tip("所选视图包含已经删除的，请重新选择","提示");
									datagrid();
								}
								cancelAllCheck();
							}
						});
				    } else if (v == 'cancel') {
				       
				    }

				    return true; //close
				};
				getTopWin(window).jBox.confirm("确定要删除数据吗？", "提示", submit);
			}else{
				getTopWin(window).getMessageBox().alert("请选择一条数据","提示");
			}

		}
	</script>
  </head>
  
  <body>
  <h2 class="title-h2" style="margin-top:0; "><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>资源视图列表</h2>
	<div class="toolbar" id = "toolbar">
	 	<a class="page_add" href="javascript:add();" >新增</a>
 		<a class="page_delete" href="javascript:batchDelete();">批量删除</a>
	</div>
	
	<div>
	<table id="viewList" width="100%" cellpadding="0" cellspacing="0" border="0" class="display">
		<thead>
			<tr>
				<th width="1%">&nbsp;&nbsp;<input type='checkbox' name='dt_check_all'
					id="dt_check_all" onclick="allClick()" /></th>
				<th width="2%">操作</th>
				<th width="25%">视图名称</th>
				<th width="8%">打开层数</th>
			</tr>
		</thead>
	</table>
	</div>
  </body>
</html>
